home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tk4.0 / tkCanvText.c < prev    next >
C/C++ Source or Header  |  1995-05-25  |  49KB  |  1,609 lines

  1. /* 
  2.  * tkCanvText.c --
  3.  *
  4.  *    This file implements text items for canvas widgets.
  5.  *
  6.  * Copyright (c) 1991-1994 The Regents of the University of California.
  7.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  */
  12.  
  13. static char sccsid[] = "@(#) tkCanvText.c 1.48 95/05/25 13:23:37";
  14.  
  15. #include <stdio.h>
  16. #include "tkInt.h"
  17. #include "tkCanvas.h"
  18. #include "tkPort.h"
  19.  
  20. /*
  21.  * One of the following structures is kept for each line of text
  22.  * in a text item.  It contains geometry and display information
  23.  * for that line.
  24.  */
  25.  
  26. typedef struct TextLine {
  27.     char *firstChar;        /* Pointer to the first character in this
  28.                  * line (in the "text" field of enclosing
  29.                  * text item). */
  30.     int numChars;        /* Number of characters displayed in this
  31.                  * line. */
  32.     int totalChars;        /* Total number of characters included as
  33.                  * part of this line (may include an extra
  34.                  * space character at the end that isn't
  35.                  * displayed). */
  36.     int x, y;            /* Origin at which to draw line on screen
  37.                  * (in integer pixel units, but in canvas
  38.                  * coordinates, not screen coordinates). */
  39.     int x1, y1;            /* Upper-left pixel that is part of text
  40.                  * line on screen (again, in integer canvas
  41.                  * pixel units). */
  42.     int x2, y2;            /* Lower-left pixel that is part of text
  43.                  * line on screen (again, in integer canvas
  44.                  * pixel units). */
  45. } TextLine;
  46.  
  47. /*
  48.  * The structure below defines the record for each text item.
  49.  */
  50.  
  51. typedef struct TextItem  {
  52.     Tk_Item header;        /* Generic stuff that's the same for all
  53.                  * types.  MUST BE FIRST IN STRUCTURE. */
  54.     Tk_CanvasTextInfo *textInfoPtr;
  55.                 /* Pointer to a structure containing
  56.                  * information about the selection and
  57.                  * insertion cursor.  The structure is owned
  58.                  * by (and shared with) the generic canvas
  59.                  * code. */
  60.     char *text;            /* Text for item (malloc-ed). */
  61.     int numChars;        /* Number of non-NULL characters in text. */
  62.     double x, y;        /* Positioning point for text. */
  63.     Tk_Anchor anchor;        /* Where to anchor text relative to (x,y). */
  64.     int width;            /* Width of lines for word-wrap, pixels.
  65.                  * Zero means no word-wrap. */
  66.     Tk_Justify justify;        /* Justification mode for text. */
  67.     int rightEdge;        /* Pixel just to right of right edge of
  68.                  * area of text item.  Used for selecting
  69.                  * up to end of line. */
  70.     XFontStruct *fontPtr;    /* Font for drawing text. */
  71.     XColor *color;        /* Color for text. */
  72.     Pixmap stipple;        /* Stipple bitmap for text, or None. */
  73.     GC gc;            /* Graphics context for drawing text. */
  74.     TextLine *linePtr;        /* Pointer to array of structures describing
  75.                  * individual lines of text item (malloc-ed). */
  76.     int numLines;        /* Number of structs at *linePtr. */
  77.     int insertPos;        /* Insertion cursor is displayed just to left
  78.                  * of character with this index. */
  79.     GC cursorOffGC;        /* If not None, this gives a graphics context
  80.                  * to use to draw the insertion cursor when
  81.                  * it's off.  Usedif the selection and
  82.                  * insertion cursor colors are the same.  */
  83.     GC selTextGC;        /* Graphics context for selected text. */
  84. } TextItem;
  85.  
  86. /*
  87.  * Information used for parsing configuration specs:
  88.  */
  89.  
  90. static Tk_ConfigSpec configSpecs[] = {
  91.     {TK_CONFIG_ANCHOR, "-anchor", (char *) NULL, (char *) NULL,
  92.     "center", Tk_Offset(TextItem, anchor),
  93.     TK_CONFIG_DONT_SET_DEFAULT},
  94.     {TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,
  95.     "black", Tk_Offset(TextItem, color), 0},
  96.     {TK_CONFIG_FONT, "-font", (char *) NULL, (char *) NULL,
  97.     "-Adobe-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*",
  98.     Tk_Offset(TextItem, fontPtr), 0},
  99.     {TK_CONFIG_JUSTIFY, "-justify", (char *) NULL, (char *) NULL,
  100.     "left", Tk_Offset(TextItem, justify),
  101.     TK_CONFIG_DONT_SET_DEFAULT},
  102.     {TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,
  103.     (char *) NULL, Tk_Offset(TextItem, stipple), TK_CONFIG_NULL_OK},
  104.     {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
  105.     (char *) NULL, 0, TK_CONFIG_NULL_OK, &tk_CanvasTagsOption},
  106.     {TK_CONFIG_STRING, "-text", (char *) NULL, (char *) NULL,
  107.     "", Tk_Offset(TextItem, text), 0},
  108.     {TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,
  109.     "0", Tk_Offset(TextItem, width), TK_CONFIG_DONT_SET_DEFAULT},
  110.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  111.     (char *) NULL, 0, 0}
  112. };
  113.  
  114. /*
  115.  * Prototypes for procedures defined in this file:
  116.  */
  117.  
  118. static void        ComputeTextBbox _ANSI_ARGS_((Tk_Canvas canvas,
  119.                 TextItem *textPtr));
  120. static int        ConfigureText _ANSI_ARGS_((Tcl_Interp *interp,
  121.                 Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
  122.                 char **argv, int flags));
  123. static int        CreateText _ANSI_ARGS_((Tcl_Interp *interp,
  124.                 Tk_Canvas canvas, struct Tk_Item *itemPtr,
  125.                 int argc, char **argv));
  126. static void        DeleteText _ANSI_ARGS_((Tk_Canvas canvas,
  127.                 Tk_Item *itemPtr, Display *display));
  128. static void        DisplayText _ANSI_ARGS_((Tk_Canvas canvas,
  129.                 Tk_Item *itemPtr, Display *display, Drawable dst,
  130.                 int x, int y, int width, int height));
  131. static int        GetSelText _ANSI_ARGS_((Tk_Canvas canvas,
  132.                 Tk_Item *itemPtr, int offset, char *buffer,
  133.                 int maxBytes));
  134. static int        GetTextIndex _ANSI_ARGS_((Tcl_Interp *interp,
  135.                 Tk_Canvas canvas, Tk_Item *itemPtr,
  136.                 char *indexString, int *indexPtr));
  137. static void        LineToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
  138.                 char *string, int numChars));
  139. static void        ScaleText _ANSI_ARGS_((Tk_Canvas canvas,
  140.                 Tk_Item *itemPtr, double originX, double originY,
  141.                 double scaleX, double scaleY));
  142. static void        SetTextCursor _ANSI_ARGS_((Tk_Canvas canvas,
  143.                 Tk_Item *itemPtr, int index));
  144. static int        TextCoords _ANSI_ARGS_((Tcl_Interp *interp,
  145.                 Tk_Canvas canvas, Tk_Item *itemPtr,
  146.                 int argc, char **argv));
  147. static void        TextDeleteChars _ANSI_ARGS_((Tk_Canvas canvas,
  148.                 Tk_Item *itemPtr, int first, int last));
  149. static void        TextInsert _ANSI_ARGS_((Tk_Canvas canvas,
  150.                 Tk_Item *itemPtr, int beforeThis, char *string));
  151. static int        TextToArea _ANSI_ARGS_((Tk_Canvas canvas,
  152.                 Tk_Item *itemPtr, double *rectPtr));
  153. static double        TextToPoint _ANSI_ARGS_((Tk_Canvas canvas,
  154.                 Tk_Item *itemPtr, double *pointPtr));
  155. static int        TextToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
  156.                 Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
  157. static void        TranslateText _ANSI_ARGS_((Tk_Canvas canvas,
  158.                 Tk_Item *itemPtr, double deltaX, double deltaY));
  159.  
  160. /*
  161.  * The structures below defines the rectangle and oval item types
  162.  * by means of procedures that can be invoked by generic item code.
  163.  */
  164.  
  165. Tk_ItemType tkTextType = {
  166.     "text",                /* name */
  167.     sizeof(TextItem),            /* itemSize */
  168.     CreateText,                /* createProc */
  169.     configSpecs,            /* configSpecs */
  170.     ConfigureText,            /* configureProc */
  171.     TextCoords,                /* coordProc */
  172.     DeleteText,                /* deleteProc */
  173.     DisplayText,            /* displayProc */
  174.     0,                    /* alwaysRedraw */
  175.     TextToPoint,            /* pointProc */
  176.     TextToArea,                /* areaProc */
  177.     TextToPostscript,            /* postscriptProc */
  178.     ScaleText,                /* scaleProc */
  179.     TranslateText,            /* translateProc */
  180.     GetTextIndex,            /* indexProc */
  181.     SetTextCursor,            /* icursorProc */
  182.     GetSelText,                /* selectionProc */
  183.     TextInsert,                /* insertProc */
  184.     TextDeleteChars,            /* dTextProc */
  185.     (Tk_ItemType *) NULL        /* nextPtr */
  186. };
  187.  
  188. /*
  189.  *--------------------------------------------------------------
  190.  *
  191.  * CreateText --
  192.  *
  193.  *    This procedure is invoked to create a new text item
  194.  *    in a canvas.
  195.  *
  196.  * Results:
  197.  *    A standard Tcl return value.  If an error occurred in
  198.  *    creating the item then an error message is left in
  199.  *    interp->result;  in this case itemPtr is left uninitialized
  200.  *    so it can be safely freed by the caller.
  201.  *
  202.  * Side effects:
  203.  *    A new text item is created.
  204.  *
  205.  *--------------------------------------------------------------
  206.  */
  207.  
  208. static int
  209. CreateText(interp, canvas, itemPtr, argc, argv)
  210.     Tcl_Interp *interp;            /* Interpreter for error reporting. */
  211.     Tk_Canvas canvas;            /* Canvas to hold new item. */
  212.     Tk_Item *itemPtr;            /* Record to hold new item;  header
  213.                      * has been initialized by caller. */
  214.     int argc;                /* Number of arguments in argv. */
  215.     char **argv;            /* Arguments describing rectangle. */
  216. {
  217.     TextItem *textPtr = (TextItem *) itemPtr;
  218.  
  219.     if (argc < 2) {
  220.     Tcl_AppendResult(interp, "wrong # args:  should be \"",
  221.         Tk_PathName(Tk_CanvasTkwin(canvas)), "\" create ",
  222.         itemPtr->typePtr->name, " x y [options]", (char *) NULL);
  223.     return TCL_ERROR;
  224.     }
  225.  
  226.     /*
  227.      * Carry out initialization that is needed in order to clean
  228.      * up after errors during the the remainder of this procedure.
  229.      */
  230.  
  231.     textPtr->text = NULL;
  232.     textPtr->textInfoPtr = Tk_CanvasGetTextInfo(canvas);
  233.     textPtr->numChars = 0;
  234.     textPtr->anchor = TK_ANCHOR_CENTER;
  235.     textPtr->width = 0;
  236.     textPtr->justify = TK_JUSTIFY_LEFT;
  237.     textPtr->rightEdge = 0;
  238.     textPtr->fontPtr = NULL;
  239.     textPtr->color = NULL;
  240.     textPtr->stipple = None;
  241.     textPtr->gc = None;
  242.     textPtr->linePtr = NULL;
  243.     textPtr->numLines = 0;
  244.     textPtr->insertPos = 0;
  245.     textPtr->cursorOffGC = None;
  246.     textPtr->selTextGC = None;
  247.  
  248.     /*
  249.      * Process the arguments to fill in the item record.
  250.      */
  251.  
  252.     if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &textPtr->x) != TCL_OK)
  253.         || (Tk_CanvasGetCoord(interp, canvas, argv[1], &textPtr->y)
  254.         != TCL_OK)) {
  255.     return TCL_ERROR;
  256.     }
  257.  
  258.     if (ConfigureText(interp, canvas, itemPtr, argc-2, argv+2, 0) != TCL_OK) {
  259.     DeleteText(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
  260.     return TCL_ERROR;
  261.     }
  262.     return TCL_OK;
  263. }
  264.  
  265. /*
  266.  *--------------------------------------------------------------
  267.  *
  268.  * TextCoords --
  269.  *
  270.  *    This procedure is invoked to process the "coords" widget
  271.  *    command on text items.  See the user documentation for
  272.  *    details on what it does.
  273.  *
  274.  * Results:
  275.  *    Returns TCL_OK or TCL_ERROR, and sets interp->result.
  276.  *
  277.  * Side effects:
  278.  *    The coordinates for the given item may be changed.
  279.  *
  280.  *--------------------------------------------------------------
  281.  */
  282.  
  283. static int
  284. TextCoords(interp, canvas, itemPtr, argc, argv)
  285.     Tcl_Interp *interp;            /* Used for error reporting. */
  286.     Tk_Canvas canvas;            /* Canvas containing item. */
  287.     Tk_Item *itemPtr;            /* Item whose coordinates are to be
  288.                      * read or modified. */
  289.     int argc;                /* Number of coordinates supplied in
  290.                      * argv. */
  291.     char **argv;            /* Array of coordinates: x1, y1,
  292.                      * x2, y2, ... */
  293. {
  294.     TextItem *textPtr = (TextItem *) itemPtr;
  295.     char x[TCL_DOUBLE_SPACE], y[TCL_DOUBLE_SPACE];
  296.  
  297.     if (argc == 0) {
  298.     Tcl_PrintDouble(interp, textPtr->x, x);
  299.     Tcl_PrintDouble(interp, textPtr->y, y);
  300.     Tcl_AppendResult(interp, x, " ", y, (char *) NULL);
  301.     } else if (argc == 2) {
  302.     if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &textPtr->x) != TCL_OK)
  303.         || (Tk_CanvasGetCoord(interp, canvas, argv[1],
  304.             &textPtr->y) != TCL_OK)) {
  305.         return TCL_ERROR;
  306.     }
  307.     ComputeTextBbox(canvas, textPtr);
  308.     } else {
  309.     sprintf(interp->result,
  310.         "wrong # coordinates:  expected 0 or 2, got %d", argc);
  311.     return TCL_ERROR;
  312.     }
  313.     return TCL_OK;
  314. }
  315.  
  316. /*
  317.  *--------------------------------------------------------------
  318.  *
  319.  * ConfigureText --
  320.  *
  321.  *    This procedure is invoked to configure various aspects
  322.  *    of a text item, such as its border and background colors.
  323.  *
  324.  * Results:
  325.  *    A standard Tcl result code.  If an error occurs, then
  326.  *    an error message is left in interp->result.
  327.  *
  328.  * Side effects:
  329.  *    Configuration information, such as colors and stipple
  330.  *    patterns, may be set for itemPtr.
  331.  *
  332.  *--------------------------------------------------------------
  333.  */
  334.  
  335. static int
  336. ConfigureText(interp, canvas, itemPtr, argc, argv, flags)
  337.     Tcl_Interp *interp;        /* Interpreter for error reporting. */
  338.     Tk_Canvas canvas;        /* Canvas containing itemPtr. */
  339.     Tk_Item *itemPtr;        /* Rectangle item to reconfigure. */
  340.     int argc;            /* Number of elements in argv.  */
  341.     char **argv;        /* Arguments describing things to configure. */
  342.     int flags;            /* Flags to pass to Tk_ConfigureWidget. */
  343. {
  344.     TextItem *textPtr = (TextItem *) itemPtr;
  345.     XGCValues gcValues;
  346.     GC newGC, newSelGC;
  347.     unsigned long mask;
  348.     Tk_Window tkwin;
  349.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  350.     XColor *selBgColorPtr;
  351.  
  352.     tkwin = Tk_CanvasTkwin(canvas);
  353.     if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, argv,
  354.         (char *) textPtr, flags) != TCL_OK) {
  355.     return TCL_ERROR;
  356.     }
  357.  
  358.     /*
  359.      * A few of the options require additional processing, such as
  360.      * graphics contexts.
  361.      */
  362.  
  363.     textPtr->numChars = strlen(textPtr->text);
  364.     newGC = newSelGC = None;
  365.     if ((textPtr->color != NULL) && (textPtr->fontPtr != NULL)) {
  366.     gcValues.foreground = textPtr->color->pixel;
  367.     gcValues.font = textPtr->fontPtr->fid;
  368.     mask = GCForeground|GCFont;
  369.     if (textPtr->stipple != None) {
  370.         gcValues.stipple = textPtr->stipple;
  371.         gcValues.fill_style = FillStippled;
  372.         mask |= GCForeground|GCStipple|GCFillStyle;
  373.     }
  374.     newGC = Tk_GetGC(tkwin, mask, &gcValues);
  375.     gcValues.foreground = textInfoPtr->selFgColorPtr->pixel;
  376.     newSelGC = Tk_GetGC(tkwin, mask, &gcValues);
  377.     }
  378.     if (textPtr->gc != None) {
  379.     Tk_FreeGC(Tk_Display(tkwin), textPtr->gc);
  380.     }
  381.     textPtr->gc = newGC;
  382.     if (textPtr->selTextGC != None) {
  383.     Tk_FreeGC(Tk_Display(tkwin), textPtr->selTextGC);
  384.     }
  385.     textPtr->selTextGC = newSelGC;
  386.  
  387.     selBgColorPtr = Tk_3DBorderColor(textInfoPtr->selBorder);
  388.     if (Tk_3DBorderColor(textInfoPtr->insertBorder)->pixel
  389.         == selBgColorPtr->pixel) {
  390.     if (selBgColorPtr->pixel == BlackPixelOfScreen(Tk_Screen(tkwin))) {
  391.         gcValues.foreground = WhitePixelOfScreen(Tk_Screen(tkwin));
  392.     } else {
  393.         gcValues.foreground = BlackPixelOfScreen(Tk_Screen(tkwin));
  394.     }
  395.     newGC = Tk_GetGC(tkwin, GCForeground, &gcValues);
  396.     } else {
  397.     newGC = None;
  398.     }
  399.     if (textPtr->cursorOffGC != None) {
  400.     Tk_FreeGC(Tk_Display(tkwin), textPtr->cursorOffGC);
  401.     }
  402.     textPtr->cursorOffGC = newGC;
  403.  
  404.     /*
  405.      * If the text was changed, move the selection and insertion indices
  406.      * to keep them inside the item.
  407.      */
  408.  
  409.     if (textInfoPtr->selItemPtr == itemPtr) {
  410.     if (textInfoPtr->selectFirst >= textPtr->numChars) {
  411.         textInfoPtr->selItemPtr = NULL;
  412.     } else {
  413.         if (textInfoPtr->selectLast >= textPtr->numChars) {
  414.         textInfoPtr->selectLast = textPtr->numChars-1;
  415.         }
  416.         if ((textInfoPtr->anchorItemPtr == itemPtr)
  417.             && (textInfoPtr->selectAnchor >= textPtr->numChars)) {
  418.         textInfoPtr->selectAnchor = textPtr->numChars-1;
  419.         }
  420.     }
  421.     }
  422.     if (textPtr->insertPos >= textPtr->numChars) {
  423.     textPtr->insertPos = textPtr->numChars;
  424.     }
  425.  
  426.     ComputeTextBbox(canvas, textPtr);
  427.     return TCL_OK;
  428. }
  429.  
  430. /*
  431.  *--------------------------------------------------------------
  432.  *
  433.  * DeleteText --
  434.  *
  435.  *    This procedure is called to clean up the data structure
  436.  *    associated with a text item.
  437.  *
  438.  * Results:
  439.  *    None.
  440.  *
  441.  * Side effects:
  442.  *    Resources associated with itemPtr are released.
  443.  *
  444.  *--------------------------------------------------------------
  445.  */
  446.  
  447. static void
  448. DeleteText(canvas, itemPtr, display)
  449.     Tk_Canvas canvas;            /* Info about overall canvas widget. */
  450.     Tk_Item *itemPtr;            /* Item that is being deleted. */
  451.     Display *display;            /* Display containing window for
  452.                      * canvas. */
  453. {
  454.     TextItem *textPtr = (TextItem *) itemPtr;
  455.  
  456.     if (textPtr->text != NULL) {
  457.     ckfree(textPtr->text);
  458.     }
  459.     if (textPtr->fontPtr != NULL) {
  460.     Tk_FreeFontStruct(textPtr->fontPtr);
  461.     }
  462.     if (textPtr->color != NULL) {
  463.     Tk_FreeColor(textPtr->color);
  464.     }
  465.     if (textPtr->stipple != None) {
  466.     Tk_FreeBitmap(display, textPtr->stipple);
  467.     }
  468.     if (textPtr->gc != None) {
  469.     Tk_FreeGC(display, textPtr->gc);
  470.     }
  471.     if (textPtr->linePtr != NULL) {
  472.     ckfree((char *) textPtr->linePtr);
  473.     }
  474.     if (textPtr->cursorOffGC != None) {
  475.     Tk_FreeGC(display, textPtr->cursorOffGC);
  476.     }
  477.     if (textPtr->selTextGC != None) {
  478.     Tk_FreeGC(display, textPtr->selTextGC);
  479.     }
  480. }
  481.  
  482. /*
  483.  *--------------------------------------------------------------
  484.  *
  485.  * ComputeTextBbox --
  486.  *
  487.  *    This procedure is invoked to compute the bounding box of
  488.  *    all the pixels that may be drawn as part of a text item.
  489.  *    In addition, it recomputes all of the geometry information
  490.  *    used to display a text item or check for mouse hits.
  491.  *
  492.  * Results:
  493.  *    None.
  494.  *
  495.  * Side effects:
  496.  *    The fields x1, y1, x2, and y2 are updated in the header
  497.  *    for itemPtr, and the linePtr structure is regenerated
  498.  *    for itemPtr.
  499.  *
  500.  *--------------------------------------------------------------
  501.  */
  502.  
  503. static void
  504. ComputeTextBbox(canvas, textPtr)
  505.     Tk_Canvas canvas;            /* Canvas that contains item. */
  506.     TextItem *textPtr;            /* Item whose bbos is to be
  507.                      * recomputed. */
  508. {
  509.     TextLine *linePtr;
  510. #define MAX_LINES 100
  511.     char *lineStart[MAX_LINES];
  512.     int lineChars[MAX_LINES];
  513.     int linePixels[MAX_LINES];
  514.     int numLines, wrapPixels, maxLinePixels, leftX, topY, y;
  515.     int lineHeight, i, fudge;
  516.     char *p;
  517.     XCharStruct *maxBoundsPtr = &textPtr->fontPtr->max_bounds;
  518.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  519.  
  520.     if (textPtr->linePtr != NULL) {
  521.     ckfree((char *) textPtr->linePtr);
  522.     textPtr->linePtr = NULL;
  523.     }
  524.  
  525.     /*
  526.      * Work through the text computing the starting point, number of
  527.      * characters, and number of pixels in each line.
  528.      */
  529.  
  530.     p = textPtr->text;
  531.     maxLinePixels = 0;
  532.     if (textPtr->width > 0) {
  533.     wrapPixels = textPtr->width;
  534.     } else {
  535.     wrapPixels = 10000000;
  536.     }
  537.     for (numLines = 0; (numLines < MAX_LINES); numLines++) {
  538.     int numChars, numPixels;
  539.     numChars = TkMeasureChars(textPtr->fontPtr, p,
  540.         (textPtr->text + textPtr->numChars) - p, 0,
  541.         wrapPixels, 0, TK_WHOLE_WORDS|TK_AT_LEAST_ONE, &numPixels);
  542.     if (numPixels > maxLinePixels) {
  543.         maxLinePixels = numPixels;
  544.     }
  545.     lineStart[numLines] = p;
  546.     lineChars[numLines] = numChars;
  547.     linePixels[numLines] = numPixels;
  548.     p += numChars;
  549.  
  550.     /*
  551.      * Skip space character that terminates a line, if there is one.
  552.      * In the case of multiple spaces, all but one will be displayed.
  553.      * This is important to make sure the insertion cursor gets
  554.      * displayed when it is in the middle of a multi-space.
  555.      */
  556.  
  557.     if (isspace(UCHAR(*p))) {
  558.         p++;
  559.     } else if (*p == 0) {
  560.         /*
  561.          * The code below is tricky.  Putting the loop termination
  562.          * here guarantees that there's a TextLine for the last
  563.          * line of text, even if the line is empty (this can
  564.          * also happen if the entire text item is empty).  This is
  565.          * needed so that we can display the insertion cursor on a
  566.          * line even when it is empty.
  567.          */
  568.  
  569.         numLines++;
  570.         break;
  571.     }
  572.     }
  573.  
  574.     /*
  575.      * Use overall geometry information to compute the top-left corner
  576.      * of the bounding box for the text item.
  577.      */
  578.  
  579.     leftX = textPtr->x + 0.5;
  580.     topY = textPtr->y + 0.5;
  581.     lineHeight = textPtr->fontPtr->ascent + textPtr->fontPtr->descent;
  582.     switch (textPtr->anchor) {
  583.     case TK_ANCHOR_NW:
  584.     case TK_ANCHOR_N:
  585.     case TK_ANCHOR_NE:
  586.         break;
  587.  
  588.     case TK_ANCHOR_W:
  589.     case TK_ANCHOR_CENTER:
  590.     case TK_ANCHOR_E:
  591.         topY -= (lineHeight * numLines)/2;
  592.         break;
  593.  
  594.     case TK_ANCHOR_SW:
  595.     case TK_ANCHOR_S:
  596.     case TK_ANCHOR_SE:
  597.         topY -= lineHeight * numLines;
  598.         break;
  599.     }
  600.     switch (textPtr->anchor) {
  601.     case TK_ANCHOR_NW:
  602.     case TK_ANCHOR_W:
  603.     case TK_ANCHOR_SW:
  604.         break;
  605.  
  606.     case TK_ANCHOR_N:
  607.     case TK_ANCHOR_CENTER:
  608.     case TK_ANCHOR_S:
  609.         leftX -= maxLinePixels/2;
  610.         break;
  611.  
  612.     case TK_ANCHOR_NE:
  613.     case TK_ANCHOR_E:
  614.     case TK_ANCHOR_SE:
  615.         leftX -= maxLinePixels;
  616.         break;
  617.     }
  618.     textPtr->rightEdge = leftX + maxLinePixels;
  619.  
  620.     /*
  621.      * Create the new TextLine array and fill it in using the geometry
  622.      * information gathered already.
  623.      */
  624.  
  625.     if (numLines > 0) {
  626.     textPtr->linePtr = (TextLine *) ckalloc((unsigned)
  627.         (numLines * sizeof(TextLine)));
  628.     } else {
  629.     textPtr->linePtr = NULL;
  630.     }
  631.     textPtr->numLines = numLines;
  632.     for (i = 0, linePtr = textPtr->linePtr, y = topY;
  633.         i < numLines; i++, linePtr++, y += lineHeight) {
  634.     linePtr->firstChar = lineStart[i];
  635.     linePtr->numChars = lineChars[i];
  636.     if (i == (numLines-1)) {
  637.         linePtr->totalChars = linePtr->numChars;
  638.     } else {
  639.         linePtr->totalChars = lineStart[i+1] - lineStart[i];
  640.     }
  641.     switch (textPtr->justify) {
  642.         case TK_JUSTIFY_LEFT:
  643.         linePtr->x = leftX;
  644.         break;
  645.         case TK_JUSTIFY_CENTER:
  646.         linePtr->x = leftX + maxLinePixels/2 - linePixels[i]/2;
  647.         break;
  648.         case TK_JUSTIFY_RIGHT:
  649.         linePtr->x = leftX + maxLinePixels - linePixels[i];
  650.         break;
  651.     }
  652.     linePtr->y = y + textPtr->fontPtr->ascent;
  653.     linePtr->x1 = linePtr->x + maxBoundsPtr->lbearing;
  654.     linePtr->y1 = y;
  655.     linePtr->x2 = linePtr->x + linePixels[i];
  656.     linePtr->y2 = linePtr->y + textPtr->fontPtr->descent - 1;
  657.     }
  658.  
  659.     /*
  660.      * Last of all, update the bounding box for the item.  The item's
  661.      * bounding box includes the bounding box of all its lines, plus
  662.      * an extra fudge factor for the cursor border (which could
  663.      * potentially be quite large).
  664.      */
  665.  
  666.     linePtr = textPtr->linePtr;
  667.     textPtr->header.x1 = textPtr->header.x2 = leftX;
  668.     textPtr->header.y1 = topY;
  669.     textPtr->header.y2 = topY + numLines*lineHeight;
  670.     for (linePtr = textPtr->linePtr, i = textPtr->numLines; i > 0;
  671.         i--, linePtr++) {
  672.     if (linePtr->x1 < textPtr->header.x1) {
  673.         textPtr->header.x1 = linePtr->x1;
  674.     }
  675.     if (linePtr->x2 >= textPtr->header.x2) {
  676.         textPtr->header.x2 = linePtr->x2 + 1;
  677.     }
  678.     }
  679.  
  680.     fudge = (textInfoPtr->insertWidth+1)/2;
  681.     if (textInfoPtr->selBorderWidth > fudge) {
  682.     fudge = textInfoPtr->selBorderWidth;
  683.     }
  684.     textPtr->header.x1 -= fudge;
  685.     textPtr->header.x2 += fudge;
  686. }
  687.  
  688. /*
  689.  *--------------------------------------------------------------
  690.  *
  691.  * DisplayText --
  692.  *
  693.  *    This procedure is invoked to draw a text item in a given
  694.  *    drawable.
  695.  *
  696.  * Results:
  697.  *    None.
  698.  *
  699.  * Side effects:
  700.  *    ItemPtr is drawn in drawable using the transformation
  701.  *    information in canvas.
  702.  *
  703.  *--------------------------------------------------------------
  704.  */
  705.  
  706. static void
  707. DisplayText(canvas, itemPtr, display, drawable, x, y, width, height)
  708.     Tk_Canvas canvas;            /* Canvas that contains item. */
  709.     Tk_Item *itemPtr;            /* Item to be displayed. */
  710.     Display *display;            /* Display on which to draw item. */
  711.     Drawable drawable;            /* Pixmap or window in which to draw
  712.                      * item. */
  713.     int x, y, width, height;        /* Describes region of canvas that
  714.                      * must be redisplayed (not used). */
  715. {
  716.     TextItem *textPtr = (TextItem *) itemPtr;
  717.     TextLine *linePtr;
  718.     int i, focusHere, insertX, insertIndex, lineIndex, tabOrigin;
  719.     int beforeSelect, inSelect, afterSelect, selStartX, selEndX;
  720.     short drawableX, drawableY;
  721.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  722.     Tk_Window tkwin = Tk_CanvasTkwin(canvas);
  723.  
  724.     if (textPtr->gc == None) {
  725.     return;
  726.     }
  727.  
  728.     /*
  729.      * If we're stippling, then modify the stipple offset in the GC.  Be
  730.      * sure to reset the offset when done, since the GC is supposed to be
  731.      * read-only.
  732.      */
  733.  
  734.     if (textPtr->stipple != None) {
  735.     Tk_CanvasSetStippleOrigin(canvas, textPtr->gc);
  736.     }
  737.  
  738.     focusHere = (textInfoPtr->focusItemPtr == itemPtr) &&
  739.         (textInfoPtr->gotFocus);
  740.     for (linePtr = textPtr->linePtr, i = textPtr->numLines;
  741.         i > 0; linePtr++, i--) {
  742.  
  743.     /*
  744.      * If part or all of this line is selected, then draw a special
  745.      * background under the selected part of the line.
  746.      */
  747.  
  748.     lineIndex = linePtr->firstChar - textPtr->text;
  749.     if ((textInfoPtr->selItemPtr != itemPtr)
  750.         || (textInfoPtr->selectLast < lineIndex)
  751.         || (textInfoPtr->selectFirst >= (lineIndex
  752.             + linePtr->totalChars))) {
  753.         beforeSelect = linePtr->numChars;
  754.         inSelect = 0;
  755.     } else {
  756.         beforeSelect = textInfoPtr->selectFirst - lineIndex;
  757.         if (beforeSelect <= 0) {
  758.         beforeSelect = 0;
  759.         selStartX = linePtr->x;
  760.         } else {
  761.         (void) TkMeasureChars(textPtr->fontPtr,
  762.             linePtr->firstChar, beforeSelect, 0,
  763.             (int) 1000000, 0, TK_PARTIAL_OK, &selStartX);
  764.         selStartX += linePtr->x;
  765.         }
  766.         inSelect = textInfoPtr->selectLast + 1 - (lineIndex + beforeSelect);
  767.  
  768.         /*
  769.          * If the selection spans the end of this line, then display
  770.          * selection background all the way to the end of the line.
  771.          * However, for the last line we only want to display up to
  772.          * the last character, not the end of the line, hence the
  773.          * "i != 1" check.
  774.          */
  775.  
  776.         if (inSelect >= (linePtr->totalChars - beforeSelect)) {
  777.         inSelect = linePtr->numChars - beforeSelect;
  778.         if (i != 1) {
  779.             selEndX = textPtr->rightEdge;
  780.             goto fillSelectBackground;
  781.         }
  782.         }
  783.         (void) TkMeasureChars(textPtr->fontPtr,
  784.             linePtr->firstChar + beforeSelect, inSelect,
  785.             selStartX-linePtr->x, (int) 1000000, 0, TK_PARTIAL_OK,
  786.             &selEndX);
  787.         selEndX += linePtr->x;
  788.         fillSelectBackground:
  789.         Tk_CanvasDrawableCoords(canvas,
  790.             (double) (selStartX - textInfoPtr->selBorderWidth),
  791.             (double) (linePtr->y - textPtr->fontPtr->ascent),
  792.             &drawableX, &drawableY);
  793.         Tk_Fill3DRectangle(tkwin, drawable, textInfoPtr->selBorder,
  794.             drawableX, drawableY,
  795.             selEndX - selStartX + 2*textInfoPtr->selBorderWidth,
  796.             textPtr->fontPtr->ascent + textPtr->fontPtr->descent,
  797.             textInfoPtr->selBorderWidth, TK_RELIEF_RAISED);
  798.     }
  799.  
  800.     /*
  801.      * If the insertion cursor is in this line, then draw a special
  802.      * background for the cursor before drawing the text.  Note:
  803.      * if we're the cursor item but the cursor is turned off, then
  804.      * redraw background over the area of the cursor.  This guarantees
  805.      * that the selection won't make the cursor invisible on mono
  806.      * displays, where both are drawn in the same color.
  807.      */
  808.  
  809.     if (focusHere) {
  810.         insertIndex = textPtr->insertPos
  811.             - (linePtr->firstChar - textPtr->text);
  812.         if ((insertIndex >= 0) && (insertIndex <= linePtr->numChars)) {
  813.         (void) TkMeasureChars(textPtr->fontPtr, linePtr->firstChar,
  814.             insertIndex, 0, (int) 1000000, 0, TK_PARTIAL_OK, &insertX);
  815.         Tk_CanvasDrawableCoords(canvas,
  816.             (double) (linePtr->x + insertX
  817.                 - (textInfoPtr->insertWidth)/2),
  818.             (double) (linePtr->y - textPtr->fontPtr->ascent),
  819.             &drawableX, &drawableY);
  820.         if (textInfoPtr->cursorOn) {
  821.             Tk_Fill3DRectangle(tkwin, drawable,
  822.                 textInfoPtr->insertBorder, drawableX, drawableY,
  823.                 textInfoPtr->insertWidth,
  824.                 textPtr->fontPtr->ascent
  825.                 + textPtr->fontPtr->descent,
  826.                 textInfoPtr->insertBorderWidth, TK_RELIEF_RAISED);
  827.         } else if (textPtr->cursorOffGC != None) {
  828.             /* Redraw the background over the area of the cursor,
  829.              * even though the cursor is turned off.  This guarantees
  830.              * that the selection won't make the cursor invisible on
  831.              * mono displays, where both may be drawn in the same
  832.              * color.
  833.              */
  834.  
  835.             XFillRectangle(display, drawable, textPtr->cursorOffGC,
  836.                 drawableX, drawableY,
  837.                 (unsigned) textInfoPtr->insertWidth,
  838.                 (unsigned) (textPtr->fontPtr->ascent
  839.                 + textPtr->fontPtr->descent));
  840.         }
  841.         }
  842.     }
  843.  
  844.     /*
  845.      * Display the text in three pieces:  the part before the
  846.      * selection, the selected part (which needs a different graphics
  847.      * context), and the part after the selection.
  848.      */
  849.  
  850.     Tk_CanvasDrawableCoords(canvas, (double) linePtr->x,
  851.         (double) linePtr->y, &drawableX, &drawableY);
  852.     tabOrigin = drawableX;
  853.     if (beforeSelect != 0) {
  854.         TkDisplayChars(display, drawable, textPtr->gc, textPtr->fontPtr,
  855.             linePtr->firstChar, beforeSelect, drawableX,
  856.             drawableY, tabOrigin, 0);
  857.     }
  858.     if (inSelect != 0) {
  859.         Tk_CanvasDrawableCoords(canvas, (double) selStartX,
  860.             (double) linePtr->y, &drawableX, &drawableY);
  861.         TkDisplayChars(display, drawable, textPtr->selTextGC,
  862.             textPtr->fontPtr, linePtr->firstChar + beforeSelect,
  863.             inSelect, drawableX, drawableY, tabOrigin, 0);
  864.     }
  865.     afterSelect = linePtr->numChars - beforeSelect - inSelect;
  866.     if (afterSelect > 0) {
  867.         Tk_CanvasDrawableCoords(canvas, (double) selEndX,
  868.             (double) linePtr->y, &drawableX, &drawableY);
  869.         TkDisplayChars(display, drawable, textPtr->gc, textPtr->fontPtr,
  870.             linePtr->firstChar + beforeSelect + inSelect,
  871.             afterSelect, drawableX, drawableY, tabOrigin, 0);
  872.     }
  873.     }
  874.     if (textPtr->stipple != None) {
  875.     XSetTSOrigin(display, textPtr->gc, 0, 0);
  876.     }
  877. }
  878.  
  879. /*
  880.  *--------------------------------------------------------------
  881.  *
  882.  * TextInsert --
  883.  *
  884.  *    Insert characters into a text item at a given position.
  885.  *
  886.  * Results:
  887.  *    None.
  888.  *
  889.  * Side effects:
  890.  *    The text in the given item is modified.  The cursor and
  891.  *    selection positions are also modified to reflect the
  892.  *    insertion.
  893.  *
  894.  *--------------------------------------------------------------
  895.  */
  896.  
  897. static void
  898. TextInsert(canvas, itemPtr, beforeThis, string)
  899.     Tk_Canvas canvas;        /* Canvas containing text item. */
  900.     Tk_Item *itemPtr;        /* Text item to be modified. */
  901.     int beforeThis;        /* Index of character before which text is
  902.                  * to be inserted. */
  903.     char *string;        /* New characters to be inserted. */
  904. {
  905.     TextItem *textPtr = (TextItem *) itemPtr;
  906.     int length;
  907.     char *new;
  908.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  909.  
  910.     length = strlen(string);
  911.     if (length == 0) {
  912.     return;
  913.     }
  914.     if (beforeThis < 0) {
  915.     beforeThis = 0;
  916.     }
  917.     if (beforeThis > textPtr->numChars) {
  918.     beforeThis = textPtr->numChars;
  919.     }
  920.  
  921.     new = (char *) ckalloc((unsigned) (textPtr->numChars + length + 1));
  922.     strncpy(new, textPtr->text, (size_t) beforeThis);
  923.     strcpy(new+beforeThis, string);
  924.     strcpy(new+beforeThis+length, textPtr->text+beforeThis);
  925.     ckfree(textPtr->text);
  926.     textPtr->text = new;
  927.     textPtr->numChars += length;
  928.  
  929.     /*
  930.      * Inserting characters invalidates indices such as those for the
  931.      * selection and cursor.  Update the indices appropriately.
  932.      */
  933.  
  934.     if (textInfoPtr->selItemPtr == itemPtr) {
  935.     if (textInfoPtr->selectFirst >= beforeThis) {
  936.         textInfoPtr->selectFirst += length;
  937.     }
  938.     if (textInfoPtr->selectLast >= beforeThis) {
  939.         textInfoPtr->selectLast += length;
  940.     }
  941.     if ((textInfoPtr->anchorItemPtr == itemPtr)
  942.         && (textInfoPtr->selectAnchor >= beforeThis)) {
  943.         textInfoPtr->selectAnchor += length;
  944.     }
  945.     }
  946.     if (textPtr->insertPos >= beforeThis) {
  947.     textPtr->insertPos += length;
  948.     }
  949.     ComputeTextBbox(canvas, textPtr);
  950. }
  951.  
  952. /*
  953.  *--------------------------------------------------------------
  954.  *
  955.  * TextDeleteChars --
  956.  *
  957.  *    Delete one or more characters from a text item.
  958.  *
  959.  * Results:
  960.  *    None.
  961.  *
  962.  * Side effects:
  963.  *    Characters between "first" and "last", inclusive, get
  964.  *    deleted from itemPtr, and things like the selection
  965.  *    position get updated.
  966.  *
  967.  *--------------------------------------------------------------
  968.  */
  969.  
  970. static void
  971. TextDeleteChars(canvas, itemPtr, first, last)
  972.     Tk_Canvas canvas;        /* Canvas containing itemPtr. */
  973.     Tk_Item *itemPtr;        /* Item in which to delete characters. */
  974.     int first;            /* Index of first character to delete. */
  975.     int last;            /* Index of last character to delete. */
  976. {
  977.     TextItem *textPtr = (TextItem *) itemPtr;
  978.     int count;
  979.     char *new;
  980.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  981.  
  982.     if (first < 0) {
  983.     first = 0;
  984.     }
  985.     if (last >= textPtr->numChars) {
  986.     last = textPtr->numChars-1;
  987.     }
  988.     if (first > last) {
  989.     return;
  990.     }
  991.     count = last + 1 - first;
  992.  
  993.     new = (char *) ckalloc((unsigned) (textPtr->numChars + 1 - count));
  994.     strncpy(new, textPtr->text, (size_t) first);
  995.     strcpy(new+first, textPtr->text+last+1);
  996.     ckfree(textPtr->text);
  997.     textPtr->text = new;
  998.     textPtr->numChars -= count;
  999.  
  1000.     /*
  1001.      * Update indexes for the selection and cursor to reflect the
  1002.      * renumbering of the remaining characters.
  1003.      */
  1004.  
  1005.     if (textInfoPtr->selItemPtr == itemPtr) {
  1006.     if (textInfoPtr->selectFirst > first) {
  1007.         textInfoPtr->selectFirst -= count;
  1008.         if (textInfoPtr->selectFirst < first) {
  1009.         textInfoPtr->selectFirst = first;
  1010.         }
  1011.     }
  1012.     if (textInfoPtr->selectLast >= first) {
  1013.         textInfoPtr->selectLast -= count;
  1014.         if (textInfoPtr->selectLast < (first-1)) {
  1015.         textInfoPtr->selectLast = (first-1);
  1016.         }
  1017.     }
  1018.     if (textInfoPtr->selectFirst > textInfoPtr->selectLast) {
  1019.         textInfoPtr->selItemPtr = NULL;
  1020.     }
  1021.     if ((textInfoPtr->anchorItemPtr == itemPtr)
  1022.         && (textInfoPtr->selectAnchor > first)) {
  1023.         textInfoPtr->selectAnchor -= count;
  1024.         if (textInfoPtr->selectAnchor < first) {
  1025.         textInfoPtr->selectAnchor = first;
  1026.         }
  1027.     }
  1028.     }
  1029.     if (textPtr->insertPos > first) {
  1030.     textPtr->insertPos -= count;
  1031.     if (textPtr->insertPos < first) {
  1032.         textPtr->insertPos = first;
  1033.     }
  1034.     }
  1035.     ComputeTextBbox(canvas, textPtr);
  1036.     return;
  1037. }
  1038.  
  1039. /*
  1040.  *--------------------------------------------------------------
  1041.  *
  1042.  * TextToPoint --
  1043.  *
  1044.  *    Computes the distance from a given point to a given
  1045.  *    text item, in canvas units.
  1046.  *
  1047.  * Results:
  1048.  *    The return value is 0 if the point whose x and y coordinates
  1049.  *    are pointPtr[0] and pointPtr[1] is inside the arc.  If the
  1050.  *    point isn't inside the arc then the return value is the
  1051.  *    distance from the point to the arc.
  1052.  *
  1053.  * Side effects:
  1054.  *    None.
  1055.  *
  1056.  *--------------------------------------------------------------
  1057.  */
  1058.  
  1059. static double
  1060. TextToPoint(canvas, itemPtr, pointPtr)
  1061.     Tk_Canvas canvas;        /* Canvas containing itemPtr. */
  1062.     Tk_Item *itemPtr;        /* Item to check against point. */
  1063.     double *pointPtr;        /* Pointer to x and y coordinates. */
  1064. {
  1065.     TextItem *textPtr = (TextItem *) itemPtr;
  1066.     TextLine *linePtr;
  1067.     int i;
  1068.     double xDiff, yDiff, dist, minDist;
  1069.  
  1070.     /*
  1071.      * Treat each line in the text item as a rectangle, compute the
  1072.      * distance to that rectangle, and take the minimum of these
  1073.      * distances.  Perform most of the calculations in integer pixel
  1074.      * units, since that's how the dimensions of the text are defined.
  1075.      */
  1076.  
  1077.     minDist = -1.0;
  1078.     for (linePtr = textPtr->linePtr, i = textPtr->numLines;
  1079.         i > 0; linePtr++, i--) {
  1080.  
  1081.     /*
  1082.      * If the point is inside the line's rectangle, then can
  1083.      * return immediately.
  1084.      */
  1085.     
  1086.     if ((pointPtr[0] >= linePtr->x1)
  1087.         && (pointPtr[0] <= linePtr->x2)
  1088.         && (pointPtr[1] >= linePtr->y1)
  1089.         && (pointPtr[1] <= linePtr->y2)) {
  1090.         return 0.0;
  1091.     }
  1092.     
  1093.     /*
  1094.      * Point is outside line's rectangle; compute distance to nearest
  1095.      * side.
  1096.      */
  1097.     
  1098.     if (pointPtr[0] < linePtr->x1) {
  1099.         xDiff = linePtr->x1 - pointPtr[0];
  1100.     } else if (pointPtr[0] > linePtr->x2)  {
  1101.         xDiff = pointPtr[0] - linePtr->x2;
  1102.     } else {
  1103.         xDiff = 0;
  1104.     }
  1105.     
  1106.     if (pointPtr[1] < linePtr->y1) {
  1107.         yDiff = linePtr->y1 - pointPtr[1];
  1108.     } else if (pointPtr[1] > linePtr->y2)  {
  1109.         yDiff = pointPtr[1] - linePtr->y2;
  1110.     } else {
  1111.         yDiff = 0;
  1112.     }
  1113.  
  1114.     dist = hypot(xDiff, yDiff);
  1115.     if ((dist < minDist) || (minDist < 0.0)) {
  1116.         minDist = dist;
  1117.     }
  1118.     }
  1119.     return minDist;
  1120. }
  1121.  
  1122. /*
  1123.  *--------------------------------------------------------------
  1124.  *
  1125.  * TextToArea --
  1126.  *
  1127.  *    This procedure is called to determine whether an item
  1128.  *    lies entirely inside, entirely outside, or overlapping
  1129.  *    a given rectangle.
  1130.  *
  1131.  * Results:
  1132.  *    -1 is returned if the item is entirely outside the area
  1133.  *    given by rectPtr, 0 if it overlaps, and 1 if it is entirely
  1134.  *    inside the given area.
  1135.  *
  1136.  * Side effects:
  1137.  *    None.
  1138.  *
  1139.  *--------------------------------------------------------------
  1140.  */
  1141.  
  1142. static int
  1143. TextToArea(canvas, itemPtr, rectPtr)
  1144.     Tk_Canvas canvas;        /* Canvas containing itemPtr. */
  1145.     Tk_Item *itemPtr;        /* Item to check against rectangle. */
  1146.     double *rectPtr;        /* Pointer to array of four coordinates
  1147.                  * (x1, y1, x2, y2) describing rectangular
  1148.                  * area.  */
  1149. {
  1150.     TextItem *textPtr = (TextItem *) itemPtr;
  1151.     TextLine *linePtr;
  1152.     int i, result;
  1153.  
  1154.     /*
  1155.      * Scan the lines one at a time, seeing whether each line is
  1156.      * entirely in, entirely out, or overlapping the rectangle.  If
  1157.      * an overlap is detected, return immediately;  otherwise wait
  1158.      * until all lines have been processed and see if they were all
  1159.      * inside or all outside.
  1160.      */
  1161.  
  1162.     result = 0;
  1163.     for (linePtr = textPtr->linePtr, i = textPtr->numLines;
  1164.         i > 0; linePtr++, i--) {
  1165.     if ((rectPtr[2] < linePtr->x1) || (rectPtr[0] > linePtr->x2)
  1166.         || (rectPtr[3] < linePtr->y1) || (rectPtr[1] > linePtr->y2)) {
  1167.         if (result == 1) {
  1168.         return 0;
  1169.         }
  1170.         result = -1;
  1171.         continue;
  1172.     }
  1173.     if ((linePtr->x1 < rectPtr[0]) || (linePtr->x2 > rectPtr[2])
  1174.         || (linePtr->y1 < rectPtr[1]) || (linePtr->y2 > rectPtr[3])) {
  1175.         return 0;
  1176.     }
  1177.     if (result == -1) {
  1178.         return 0;
  1179.     }
  1180.     result = 1;
  1181.     }
  1182.     return result;
  1183. }
  1184.  
  1185. /*
  1186.  *--------------------------------------------------------------
  1187.  *
  1188.  * ScaleText --
  1189.  *
  1190.  *    This procedure is invoked to rescale a text item.
  1191.  *
  1192.  * Results:
  1193.  *    None.
  1194.  *
  1195.  * Side effects:
  1196.  *    Scales the position of the text, but not the size
  1197.  *    of the font for the text.
  1198.  *
  1199.  *--------------------------------------------------------------
  1200.  */
  1201.  
  1202.     /* ARGSUSED */
  1203. static void
  1204. ScaleText(canvas, itemPtr, originX, originY, scaleX, scaleY)
  1205.     Tk_Canvas canvas;            /* Canvas containing rectangle. */
  1206.     Tk_Item *itemPtr;            /* Rectangle to be scaled. */
  1207.     double originX, originY;        /* Origin about which to scale rect. */
  1208.     double scaleX;            /* Amount to scale in X direction. */
  1209.     double scaleY;            /* Amount to scale in Y direction. */
  1210. {
  1211.     TextItem *textPtr = (TextItem *) itemPtr;
  1212.  
  1213.     textPtr->x = originX + scaleX*(textPtr->x - originX);
  1214.     textPtr->y = originY + scaleY*(textPtr->y - originY);
  1215.     ComputeTextBbox(canvas, textPtr);
  1216.     return;
  1217. }
  1218.  
  1219. /*
  1220.  *--------------------------------------------------------------
  1221.  *
  1222.  * TranslateText --
  1223.  *
  1224.  *    This procedure is called to move a text item by a
  1225.  *    given amount.
  1226.  *
  1227.  * Results:
  1228.  *    None.
  1229.  *
  1230.  * Side effects:
  1231.  *    The position of the text item is offset by (xDelta, yDelta),
  1232.  *    and the bounding box is updated in the generic part of the
  1233.  *    item structure.
  1234.  *
  1235.  *--------------------------------------------------------------
  1236.  */
  1237.  
  1238. static void
  1239. TranslateText(canvas, itemPtr, deltaX, deltaY)
  1240.     Tk_Canvas canvas;            /* Canvas containing item. */
  1241.     Tk_Item *itemPtr;            /* Item that is being moved. */
  1242.     double deltaX, deltaY;        /* Amount by which item is to be
  1243.                      * moved. */
  1244. {
  1245.     TextItem *textPtr = (TextItem *) itemPtr;
  1246.  
  1247.     textPtr->x += deltaX;
  1248.     textPtr->y += deltaY;
  1249.     ComputeTextBbox(canvas, textPtr);
  1250. }
  1251.  
  1252. /*
  1253.  *--------------------------------------------------------------
  1254.  *
  1255.  * GetTextIndex --
  1256.  *
  1257.  *    Parse an index into a text item and return either its value
  1258.  *    or an error.
  1259.  *
  1260.  * Results:
  1261.  *    A standard Tcl result.  If all went well, then *indexPtr is
  1262.  *    filled in with the index (into itemPtr) corresponding to
  1263.  *    string.  Otherwise an error message is left in
  1264.  *    interp->result.
  1265.  *
  1266.  * Side effects:
  1267.  *    None.
  1268.  *
  1269.  *--------------------------------------------------------------
  1270.  */
  1271.  
  1272. static int
  1273. GetTextIndex(interp, canvas, itemPtr, string, indexPtr)
  1274.     Tcl_Interp *interp;        /* Used for error reporting. */
  1275.     Tk_Canvas canvas;        /* Canvas containing item. */
  1276.     Tk_Item *itemPtr;        /* Item for which the index is being
  1277.                  * specified. */
  1278.     char *string;        /* Specification of a particular character
  1279.                  * in itemPtr's text. */
  1280.     int *indexPtr;        /* Where to store converted index. */
  1281. {
  1282.     TextItem *textPtr = (TextItem *) itemPtr;
  1283.     size_t length;
  1284.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  1285.  
  1286.     length = strlen(string);
  1287.  
  1288.     if (string[0] == 'e') {
  1289.     if (strncmp(string, "end", length) == 0) {
  1290.         *indexPtr = textPtr->numChars;
  1291.     } else {
  1292.         badIndex:
  1293.  
  1294.         /*
  1295.          * Some of the paths here leave messages in interp->result,
  1296.          * so we have to clear it out before storing our own message.
  1297.          */
  1298.  
  1299.         Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
  1300.         Tcl_AppendResult(interp, "bad index \"", string, "\"",
  1301.             (char *) NULL);
  1302.         return TCL_ERROR;
  1303.     }
  1304.     } else if (string[0] == 'i') {
  1305.     if (strncmp(string, "insert", length) == 0) {
  1306.         *indexPtr = textPtr->insertPos;
  1307.     } else {
  1308.         goto badIndex;
  1309.     }
  1310.     } else if (string[0] == 's') {
  1311.     if (textInfoPtr->selItemPtr != itemPtr) {
  1312.         interp->result = "selection isn't in item";
  1313.         return TCL_ERROR;
  1314.     }
  1315.     if (length < 5) {
  1316.         goto badIndex;
  1317.     }
  1318.     if (strncmp(string, "sel.first", length) == 0) {
  1319.         *indexPtr = textInfoPtr->selectFirst;
  1320.     } else if (strncmp(string, "sel.last", length) == 0) {
  1321.         *indexPtr = textInfoPtr->selectLast;
  1322.     } else {
  1323.         goto badIndex;
  1324.     }
  1325.     } else if (string[0] == '@') {
  1326.     int x, y, dummy, i;
  1327.     double tmp;
  1328.     char *end, *p;
  1329.     TextLine *linePtr;
  1330.  
  1331.     p = string+1;
  1332.     tmp = strtod(p, &end);
  1333.     if ((end == p) || (*end != ',')) {
  1334.         goto badIndex;
  1335.     }
  1336.     x = (tmp < 0) ? tmp - 0.5 : tmp + 0.5;
  1337.     p = end+1;
  1338.     tmp = strtod(p, &end);
  1339.     if ((end == p) || (*end != 0)) {
  1340.         goto badIndex;
  1341.     }
  1342.     y = (tmp < 0) ? tmp - 0.5 : tmp + 0.5;
  1343.     if ((textPtr->numChars == 0) || (y < textPtr->linePtr[0].y1)) {
  1344.         *indexPtr = 0;
  1345.         return TCL_OK;
  1346.     }
  1347.     for (i = 0, linePtr = textPtr->linePtr; ; i++, linePtr++) {
  1348.         if (i >= textPtr->numLines) {
  1349.         *indexPtr = textPtr->numChars;
  1350.         return TCL_OK;
  1351.         }
  1352.         if (y <= linePtr->y2) {
  1353.         break;
  1354.         }
  1355.     }
  1356.     *indexPtr = TkMeasureChars(textPtr->fontPtr, linePtr->firstChar,
  1357.         linePtr->numChars, linePtr->x, x, linePtr->x, 0, &dummy);
  1358.     *indexPtr += linePtr->firstChar - textPtr->text;
  1359.     } else {
  1360.     if (Tcl_GetInt(interp, string, indexPtr) != TCL_OK) {
  1361.         goto badIndex;
  1362.     }
  1363.     if (*indexPtr < 0){
  1364.         *indexPtr = 0;
  1365.     } else if (*indexPtr > textPtr->numChars) {
  1366.         *indexPtr = textPtr->numChars;
  1367.     }
  1368.     }
  1369.     return TCL_OK;
  1370. }
  1371.  
  1372. /*
  1373.  *--------------------------------------------------------------
  1374.  *
  1375.  * SetTextCursor --
  1376.  *
  1377.  *    Set the position of the insertion cursor in this item.
  1378.  *
  1379.  * Results:
  1380.  *    None.
  1381.  *
  1382.  * Side effects:
  1383.  *    The cursor position will change.
  1384.  *
  1385.  *--------------------------------------------------------------
  1386.  */
  1387.  
  1388.     /* ARGSUSED */
  1389. static void
  1390. SetTextCursor(canvas, itemPtr, index)
  1391.     Tk_Canvas canvas;            /* Record describing canvas widget. */
  1392.     Tk_Item *itemPtr;            /* Text item in which cursor position
  1393.                      * is to be set. */
  1394.     int index;                /* Index of character just before which
  1395.                      * cursor is to be positioned. */
  1396. {
  1397.     TextItem *textPtr = (TextItem *) itemPtr;
  1398.  
  1399.     if (index < 0) {
  1400.     textPtr->insertPos = 0;
  1401.     } else  if (index > textPtr->numChars) {
  1402.     textPtr->insertPos = textPtr->numChars;
  1403.     } else {
  1404.     textPtr->insertPos = index;
  1405.     }
  1406. }
  1407.  
  1408. /*
  1409.  *--------------------------------------------------------------
  1410.  *
  1411.  * GetSelText --
  1412.  *
  1413.  *    This procedure is invoked to return the selected portion
  1414.  *    of a text item.  It is only called when this item has
  1415.  *    the selection.
  1416.  *
  1417.  * Results:
  1418.  *    The return value is the number of non-NULL bytes stored
  1419.  *    at buffer.  Buffer is filled (or partially filled) with a
  1420.  *    NULL-terminated string containing part or all of the selection,
  1421.  *    as given by offset and maxBytes.
  1422.  *
  1423.  * Side effects:
  1424.  *    None.
  1425.  *
  1426.  *--------------------------------------------------------------
  1427.  */
  1428.  
  1429. static int
  1430. GetSelText(canvas, itemPtr, offset, buffer, maxBytes)
  1431.     Tk_Canvas canvas;            /* Canvas containing selection. */
  1432.     Tk_Item *itemPtr;            /* Text item containing selection. */
  1433.     int offset;                /* Offset within selection of first
  1434.                      * character to be returned. */
  1435.     char *buffer;            /* Location in which to place
  1436.                      * selection. */
  1437.     int maxBytes;            /* Maximum number of bytes to place
  1438.                      * at buffer, not including terminating
  1439.                      * NULL character. */
  1440. {
  1441.     TextItem *textPtr = (TextItem *) itemPtr;
  1442.     int count;
  1443.     Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
  1444.  
  1445.     count = textInfoPtr->selectLast + 1 - textInfoPtr->selectFirst - offset;
  1446.     if (textInfoPtr->selectLast == textPtr->numChars) {
  1447.     count -= 1;
  1448.     }
  1449.     if (count > maxBytes) {
  1450.     count = maxBytes;
  1451.     }
  1452.     if (count <= 0) {
  1453.     return 0;
  1454.     }
  1455.     strncpy(buffer, textPtr->text + textInfoPtr->selectFirst + offset,
  1456.         (size_t) count);
  1457.     buffer[count] = '\0';
  1458.     return count;
  1459. }
  1460.  
  1461. /*
  1462.  *--------------------------------------------------------------
  1463.  *
  1464.  * TextToPostscript --
  1465.  *
  1466.  *    This procedure is called to generate Postscript for
  1467.  *    text items.
  1468.  *
  1469.  * Results:
  1470.  *    The return value is a standard Tcl result.  If an error
  1471.  *    occurs in generating Postscript then an error message is
  1472.  *    left in interp->result, replacing whatever used
  1473.  *    to be there.  If no error occurs, then Postscript for the
  1474.  *    item is appended to the result.
  1475.  *
  1476.  * Side effects:
  1477.  *    None.
  1478.  *
  1479.  *--------------------------------------------------------------
  1480.  */
  1481.  
  1482. static int
  1483. TextToPostscript(interp, canvas, itemPtr, prepass)
  1484.     Tcl_Interp *interp;            /* Leave Postscript or error message
  1485.                      * here. */
  1486.     Tk_Canvas canvas;            /* Information about overall canvas. */
  1487.     Tk_Item *itemPtr;            /* Item for which Postscript is
  1488.                      * wanted. */
  1489.     int prepass;            /* 1 means this is a prepass to
  1490.                      * collect font information;  0 means
  1491.                      * final Postscript is being created. */
  1492. {
  1493.     TextItem *textPtr = (TextItem *) itemPtr;
  1494.     TextLine *linePtr;
  1495.     int i;
  1496.     char *xoffset = NULL, *yoffset = NULL;    /* Initializations needed */
  1497.     char *justify = NULL;            /* only to stop compiler
  1498.                             * warnings. */
  1499.     char buffer[500];
  1500.  
  1501.     if (textPtr->color == NULL) {
  1502.     return TCL_OK;
  1503.     }
  1504.  
  1505.     if (Tk_CanvasPsFont(interp, canvas, textPtr->fontPtr) != TCL_OK) {
  1506.     return TCL_ERROR;
  1507.     }
  1508.     if (Tk_CanvasPsColor(interp, canvas, textPtr->color) != TCL_OK) {
  1509.     return TCL_ERROR;
  1510.     }
  1511.     if (textPtr->stipple != None) {
  1512.     Tcl_AppendResult(interp, "/StippleText {\n    ",
  1513.         (char *) NULL);
  1514.     Tk_CanvasPsStipple(interp, canvas, textPtr->stipple);
  1515.     Tcl_AppendResult(interp, "} bind def\n", (char *) NULL);
  1516.     }
  1517.     sprintf(buffer, "%.15g %.15g [\n", textPtr->x,
  1518.         Tk_CanvasPsY(canvas, textPtr->y));
  1519.     Tcl_AppendResult(interp, buffer, (char *) NULL);
  1520.     for (i = textPtr->numLines, linePtr = textPtr->linePtr;
  1521.         i > 0; i--, linePtr++) {
  1522.     Tcl_AppendResult(interp, "    ", (char *) NULL);
  1523.     LineToPostscript(interp, linePtr->firstChar,
  1524.         linePtr->numChars);
  1525.     Tcl_AppendResult(interp, "\n", (char *) NULL);
  1526.     }
  1527.     switch (textPtr->anchor) {
  1528.     case TK_ANCHOR_NW:     xoffset = "0";    yoffset = "0";   break;
  1529.     case TK_ANCHOR_N:      xoffset = "-0.5"; yoffset = "0";   break;
  1530.     case TK_ANCHOR_NE:     xoffset = "-1";   yoffset = "0";   break;
  1531.     case TK_ANCHOR_E:      xoffset = "-1";   yoffset = "0.5"; break;
  1532.     case TK_ANCHOR_SE:     xoffset = "-1";   yoffset = "1";   break;
  1533.     case TK_ANCHOR_S:      xoffset = "-0.5"; yoffset = "1";   break;
  1534.     case TK_ANCHOR_SW:     xoffset = "0";    yoffset = "1";   break;
  1535.     case TK_ANCHOR_W:      xoffset = "0";    yoffset = "0.5"; break;
  1536.     case TK_ANCHOR_CENTER: xoffset = "-0.5"; yoffset = "0.5"; break;
  1537.     }
  1538.     switch (textPtr->justify) {
  1539.     case TK_JUSTIFY_LEFT:    justify = "0";   break;
  1540.     case TK_JUSTIFY_CENTER:    justify = "0.5"; break;
  1541.     case TK_JUSTIFY_RIGHT:    justify = "1";   break;
  1542.     }
  1543.     sprintf(buffer, "] %d %d %s %s %s %s DrawText\n",
  1544.         textPtr->width,
  1545.         textPtr->fontPtr->ascent + textPtr->fontPtr->descent,
  1546.         xoffset, yoffset, justify,
  1547.         (textPtr->stipple == None) ? "false" : "true");
  1548.     Tcl_AppendResult(interp, buffer, (char *) NULL);
  1549.     return TCL_OK;
  1550. }
  1551.  
  1552. /*
  1553.  *--------------------------------------------------------------
  1554.  *
  1555.  * LineToPostscript --
  1556.  *
  1557.  *    This procedure generates a parenthesized Postscript string
  1558.  *    describing one line of text from a text item.
  1559.  *
  1560.  * Results:
  1561.  *    None. The parenthesized string is appended to
  1562.  *    interp->result.  It generates proper backslash notation so
  1563.  *    that Postscript can interpret the string correctly.
  1564.  *
  1565.  * Side effects:
  1566.  *    None.
  1567.  *
  1568.  *--------------------------------------------------------------
  1569.  */
  1570.  
  1571. static void
  1572. LineToPostscript(interp, string, numChars)
  1573.     Tcl_Interp *interp;        /* Interp whose result is to be appended to. */
  1574.     char *string;        /* String to Postscript-ify. */
  1575.     int numChars;        /* Number of characters in the string. */
  1576. {
  1577. #define BUFFER_SIZE 100
  1578.     char buffer[BUFFER_SIZE+5];
  1579.     int used, c;
  1580.  
  1581.     buffer[0] = '(';
  1582.     used = 1;
  1583.     for ( ; numChars > 0; string++, numChars--) {
  1584.     c = (*string) & 0xff;
  1585.     if ((c == '(') || (c == ')') || (c == '\\') || (c < 0x20)
  1586.         || (c >= 0x7f)) {
  1587.         /*
  1588.          * Tricky point:  the "03" is necessary in the sprintf below,
  1589.          * so that a full three digits of octal are always generated.
  1590.          * Without the "03", a number following this sequence could
  1591.          * be interpreted by Postscript as part of this sequence.
  1592.          */
  1593.         sprintf(buffer+used, "\\%03o", c);
  1594.         used += strlen(buffer+used);
  1595.     } else {
  1596.         buffer[used] = c;
  1597.         used++;
  1598.     }
  1599.     if (used >= BUFFER_SIZE) {
  1600.         buffer[used] = 0;
  1601.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  1602.         used = 0;
  1603.     }
  1604.     }
  1605.     buffer[used] = ')';
  1606.     buffer[used+1] = 0;
  1607.     Tcl_AppendResult(interp, buffer, (char *) NULL);
  1608. }
  1609.